package com.fjl.prediction.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjl.prediction.mapper.SysUserMapper;
import com.fjl.prediction.pojo.SysUser;
import com.fjl.prediction.pojo.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserMapper sysUserMapper;


    public String getNameById(Long id){
        return sysUserMapper.getNameById(id);
    }


    public SysUser selectByUserName(String username) {
        System.out.println(baseMapper.selectByUserName(username).getPassword());
        return baseMapper.selectByUserName(username);

    }

    @Transactional
    public String insert(String username, String phone, String email, String[] roles, String pass) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<SysUser> existsList = list(wrapper);
        if (!existsList.isEmpty()) {
            return "用户已存在";
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(IdWorker.getId());
        sysUser.setEnable(1);
        sysUser.setCreateTime(System.currentTimeMillis());
        sysUser.setUsername(username);
        sysUser.setPassword(new BCryptPasswordEncoder().encode(pass));
        sysUser.setEmail(email);
        sysUser.setPhone(phone);
        if (!save(sysUser)) {
            return "用户保存失败";
        }

        if (roles == null || roles.length <= 0) {
            return "true";
        }

        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (String role : roles) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysUserId(sysUser.getId());
            sysUserRole.setSysRoleId(Long.parseLong(role));
            sysUserRoleList.add(sysUserRole);
        }
        if (!sysUserRoleService.saveBatch(sysUserRoleList)) {
            return "用户保存成功！角色配置失败";
        }
        return "true";
    }

    @Transactional
    public String update(String id, String username, String phone, String email) {
        System.out.println(id);
		UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("username",username);
        updateWrapper.set("phone",phone);
        updateWrapper.set("email",email);
		updateWrapper.eq("id",id);
        if (!update(updateWrapper)) {
            return "更新用户和用户角色失败";
        }

        return "true";

    }

    @Transactional
    public String roleUpdate(String id, String[] roles) {
        UpdateWrapper<SysUserRole> removeWrapper = new UpdateWrapper<>();
        removeWrapper.eq("sys_user_id", id);
        sysUserRoleService.remove(removeWrapper);

        if (roles == null || roles.length <= 0) {
            return "true";
        }
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (String role : roles) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setSysUserId(Long.valueOf(id));
            sysUserRole.setSysRoleId(Long.parseLong(role));
            sysUserRoleList.add(sysUserRole);
        }
        if (!sysUserRoleService.saveBatch(sysUserRoleList)) {
            return "用户保存成功！角色配置失败";
        }
        return "true";
    }
}
